// pages/order/order.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _num: 1,
    nothing:"none",
    set:''
  },
  uctime: function () {
    console.log('加载定时器')
    let that = this;
    let data = that.data.order;
    for (var i = 0; i < data.length; i++) {
      if (data[i].createtime <= 0) {
        wx.request({
          url: app.data.Url + '/spell/CollageDetection',
          data: {
          },
          success: function (res) {
          }
        })
      } else {
        var time = data[i].createtime
        var t;
        if (time == 0) {
          wx.request({
            url: app.data.Url + '/spell/CollageDetection',
            data: {
            },
            success: function (res) {
            }
          })
          clearInterval(that.data.set)
          that.ondata()
          console.log('结束')

        } else {
          let t;
          time = time - 1
          if (time > 1) {
            var day = Math.floor(time / 3600 / 24);     //天
            //console.log(day,'------------')
            var hour = Math.floor(time / 3600) % 24;       //时
            //console.log(hour,'hourhour')
            var min = Math.floor(time / 60) % 60;     //分
            var sec = time % 60;                      //秒
            t = day + '天'
            if (hour < 10) {
              t += '0' + hour + "时";
            } else {
              t += hour + "时";
            }
            if (min < 10) { t += "0"; }
            t += min + "分";
            //console.log(t, 'ttttttttt')
            if (sec < 10) { t += "0"; }
            t += sec      //.toFixed(2);
          }
          data[i].createtime = data[i].createtime - 1
          data[i].createtimes = t
          that.setData({
            order: data
          })
        }
      }
    }
  },
  focus_click: function(e){
    let that = this
    clearInterval(that.data.set)          //开始清除定时器
    wx.request({
      url: app.data.Url + 'order/getOrderLists',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        status: e.target.dataset.num,
        'type':0
      },
      method: 'get',
      success: function (res) {
        console.log(res.data)
        that.setData({
          order: res.data.data
        })
        if (e.target.dataset.num == 1){
          that.setData({ set: setInterval(function () { that.uctime() }, 1000) })
        }
      }
    })
  
    this.setData({
      _num:e.target.dataset.num
    })
  },
  //跳商品详情页
  onurl:function(e){
    wx.navigateTo({
      url: '../order_details/index?id='+e.currentTarget.dataset.id,
    })
  },
  //确认收货
  confirm:function(e){
    let that = this;
    var order = that.data.order; 
    wx.request({
      url: app.data.Url + 'Order/takeGoods',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        orderid: e.currentTarget.dataset.id,
      },
      method: 'get',
      success: function (res) {
        //console.log(res.data.code)
        if(res.data.code == 1){
          order.forEach(function (list, i) {
            //console.log(list.orderid + '----' + e.currentTarget.dataset.id)
            if (list.orderid == e.currentTarget.dataset.id){
              order.splice(i,1)
            }
          })
          that.setData({
            order: order
          })
        }
      }
    })
  },

  //退货
  retreat:function(e){
      wx.navigateTo({
        url: '../refund_apply/index?id=' + e.currentTarget.dataset.id,
      })
  },
  //退款
  refent:function(e){
    wx.navigateTo({
      url: '../refent_zhi/index?orderno='+ e.currentTarget.dataset.id,
    })
  },
  //撤销订单
  cancel:function(e){
    let that =this;
    console.log(e.currentTarget.dataset.id)
    wx.showModal({
      title: '提示',
      content: '确定要取消订单？',
      success: function (res) {
        if (res.confirm) {
            wx.showLoading({})
            wx.request({
              url: app.data.Url + 'order/cancelOrder',
              data: {
                orderid: e.currentTarget.dataset.id
              },
              method: 'post',
              success: function (rs) {
                wx.hideLoading();
                that.onShow();
                wx.showToast({
                  title: '取消成功',
                })
                wx.request({
                  url: app.data.Url + 'order/getOrderLists',
                  data: {
                    uid: wx.getStorageSync('userInfo').uid,
                    status: that.data._num,
                    type: 0
                  },
                  method: 'get',
                  success: function (res) {
                    that.setData({
                      order: res.data.data,
                    })
                    console.log(res.data.data)
                  }
                })
              }
            })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  outbuy: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定取消订单吗',
      success: function (res) {
        if (res.confirm == 1) {
          wx.showLoading({

          })
          wx.request({
            url: app.data.Url + 'spell/delOrder',
            data: {
              order_no: e.currentTarget.dataset.id
            },
            method: 'post',
            success: function (rs) {
              wx.hideLoading();
              that.onLoad();
              wx.showToast({
                title: '取消成功',
              })
            }
          })
        }
      }
    })
  },

  fukuan:function(e){
    console.log(e.currentTarget.id)
    let that=this;
    wx.request({
      url: app.data.Url + 'order/orderPayParams',
      data: {
        order_no: e.currentTarget.id,
        title: '智慧社区订单支付',
        uid: wx.getStorageSync('userInfo').uid
      },
      method: 'post',
      success: function (res) {
        console.log(res.data.data.weixin)
        wx.requestPayment({
          'timeStamp': res.data.data.weixin.timeStamp,
          'nonceStr': res.data.data.weixin.nonceStr,
          'package': res.data.data.weixin.package,
          'signType': 'MD5',
          'paySign': res.data.data.weixin.paySign,
          'success': function (res) {

          },
          'fail': function (res) {

          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    that.setData({
      _num: options.num
    })
    that.ondata()
  },
  ondata:function(){
    let that = this;
    wx.request({
      url: app.data.Url + 'order/getOrderLists',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        status: that.data._num,
        type: 0
      },
      method: 'get',
      success: function (res) {
        that.setData({
          order: res.data.data,
        })
        if (that.data._num == 1) {
          that.setData({ set: setInterval(function () { that.uctime() }, 1000) })
        }
        console.log(res.data.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }

})